/**
 * ImportData.java created by zhangzhidong 
 * at 下午9:21:05 2016年4月11日
 */
package wechat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.Statement;

/**
 * @author zhangzhidong
 *
 */
public class ImportData {
	/**
	 * 下午9:21:06 2016年4月11日
	 * @param args
	 */
	private static String inputDir = "/Users/zhangzhidong/Downloads/wechat/";
	public static void main(String[] args) throws IOException, SQLException {
		// TODO Auto-generated method stub
		String regx = "^(http.*?)\\t+(.*?)\\t+(.*?)\\t+(.*?)\\t+(.*?)\\t+(.*?)。。\t([0-9]{8})$";
		Pattern p = Pattern.compile(regx);
		String []cols = {"url","title","name","account","brief","content","date"};
		//建立database 
		String dburl  = "jdbc:mysql://202.112.147.157:3306/meetingassistant?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
		 try{   
			    Class.forName("com.mysql.jdbc.Driver") ;   
		 }catch(ClassNotFoundException e){   
			    System.out.println("找不到驱动程序类 ，加载驱动失败！");   
			    e.printStackTrace() ;
			    return ;
		 }   
		/*Connection con = null;
		try{   
			con = (Connection) DriverManager.getConnection(dburl , "meetingassistant" , "meeting" ) ;   
		}catch(SQLException se){   
			System.out.println("数据库连接失败！");   
			se.printStackTrace() ;   
			 return;
		}   
		Statement st = (Statement) con.createStatement();
		String sql =  "insert into wechat values('%d','%s','%s','%s','%s','%s','%s','%s')";
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyyMMdd");
		SimpleDateFormat parsedSDF =  new SimpleDateFormat( "yyyy-MM-dd");
		String path = inputDir+"/wechat0";
		for (int i = 0;i<1000;i++){
			File f = null;
			String delatedPath = null;
			if(i<10){
				delatedPath = path+"000";
			}else if (i>=10 && i<=99){
				delatedPath = path+"00";
			}else if(i>99 && i<=999){
				delatedPath = path+"0";
			}else{
				
			}
			f  = new File(delatedPath+i);
			BufferedReader br  =  new BufferedReader(new  FileReader(f));
			String line = br.readLine();
			Matcher m = p.matcher(line);
			if (m.find()){
				Object sqlargs []= new Object[m.groupCount()+1];
				int index = 1;
				sqlargs[0] = new Integer(i);
				for(int j=1;j<=m.groupCount();j++){
					String col  = m.group(j);
					//把内容里面的' 替换
					if (j==7){//第七列日期变换
						try {
							col = parsedSDF.format(sdf.parse(m.group(7)));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							col = "";
						}
					}
					sqlargs[index++] = escape(null, filterEmoji(col));
				}			
				String usedSql = null;
				try {
					usedSql = String.format(sql,sqlargs);
				}catch(Exception e){
					System.out.println(sqlargs[0]+"转换失败!");
					continue;
				}
				try{
					st.execute(usedSql);
					System.out.println("第"+i+"成功插入!");
				}catch(Exception e){			
					System.out.println("第"+i+"个执行异常! 原因:"+e.getMessage());
				}
			}else{
				System.out.print("第"+i+"没有匹配上!");
			}
			br.close();
		}
		con.close();*/
	}
	public static String filterEmoji(String source) {  
        if(source != null)
        {
            Pattern emoji = Pattern.compile ("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",Pattern.UNICODE_CASE | Pattern . CASE_INSENSITIVE ) ;
            Matcher emojiMatcher = emoji.matcher(source);
            if ( emojiMatcher.find()) 
            {
                source = emojiMatcher.replaceAll("*");
                return source ; 
            }
        return source;
       }
       return source;  
    }
	public static String escape(Pattern p,String in){
		String res = in.replace("'", "\\'");
		return res;
	}

}
